<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveRecord::Associations::CollectionProxy</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            ActiveRecord::Associations::CollectionProxy 
            
                <span class="parent">&lt; 
                    
                    Relation
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/activerecord/lib/active_record/associations/collection_proxy_rb.html">activerecord/lib/active_record/associations/collection_proxy.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>Association proxies in Active Record are middlemen between the object that
holds the association, known as the <code>@owner</code>, and the actual
associated object, known as the <code>@target</code>. The kind of
association any proxy is about is available in <code>@reflection</code>.
That’s an instance of the class
ActiveRecord::Reflection::AssociationReflection.</p>

<p>For example, given</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Blog</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">posts</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">blog</span> = <span class="ruby-constant">Blog</span>.<span class="ruby-identifier">first</span>
</pre>

<p>the association proxy in <code>blog.posts</code> has the object in
<code>blog</code> as <code>@owner</code>, the collection of its posts as
<code>@target</code>, and the <code>@reflection</code> object represents a
<code>:has_many</code> macro.</p>

<p>This class delegates unknown methods to <code>@target</code> via
<code>method_missing</code>.</p>

<p>The <code>@target</code> object is not loaded until needed. For example,</p>

<pre>blog.posts.count</pre>

<p>is computed directly through SQL and does not trigger by itself the
instantiation of the actual post records.</p>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>A</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-any-3F">any?</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>B</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-build">build</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-concat">concat</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-count">count</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-create">create</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-create-21">create!</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>D</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-delete">delete</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-delete_all">delete_all</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-destroy">destroy</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-destroy_all">destroy_all</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-distinct">distinct</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>E</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-empty-3F">empty?</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>F</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-find">find</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-first">first</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>I</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-include-3F">include?</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>L</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-last">last</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-length">length</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-load_target">load_target</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-loaded-3F">loaded?</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>M</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-many-3F">many?</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-new">new</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>P</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-proxy_association">proxy_association</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>R</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-replace">replace</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-scope">scope</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-scoping">scoping</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-select">select</a>,
              </li>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-size">size</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>T</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-target">target</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>U</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="CollectionProxy.html#method-i-uniq">uniq</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-any-3F">
            
              <b>any?</b>(&amp;block)
            
            <a href="CollectionProxy.html#method-i-any-3F" name="method-i-any-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns <code>true</code> if the collection is not empty.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span> <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">any?</span>  <span class="ruby-comment"># =&gt; false</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Pet</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Snoop'</span>)
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span> <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">any?</span>  <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>You can also pass a block to define criteria. The behavior is the same, it
returns true if the collection based on the criteria is not empty.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [#&lt;Pet name: &quot;Snoop&quot;, group: &quot;dogs&quot;&gt;]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">any?</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">pet</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">pet</span>.<span class="ruby-identifier">group</span> <span class="ruby-operator">==</span> <span class="ruby-string">'cats'</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># =&gt; false</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">any?</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">pet</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">pet</span>.<span class="ruby-identifier">group</span> <span class="ruby-operator">==</span> <span class="ruby-string">'dogs'</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># =&gt; true</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-any-3F_source')" id="l_method-i-any-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L780" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-any-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 780</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">any?</span>(&amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">any?</span>(&amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-build">
            
              <b>build</b>(attributes = {}, &amp;block)
            
            <a href="CollectionProxy.html#method-i-build" name="method-i-build" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a new object of the collection type that has been instantiated with
<code>attributes</code> and linked to this object, but have not yet been
saved. You can pass an array of attributes hashes, this will return an
array with the new objects.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">build</span>
<span class="ruby-comment"># =&gt; #&lt;Pet id: nil, name: nil, person_id: 1&gt;</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">build</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Fancy-Fancy'</span>)
<span class="ruby-comment"># =&gt; #&lt;Pet id: nil, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">build</span>([{<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Spook'</span>}, {<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Choo-Choo'</span>}, {<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Brain'</span>}])
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet id: nil, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: nil, name: &quot;Choo-Choo&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: nil, name: &quot;Brain&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span>  <span class="ruby-comment"># =&gt; 5 # size of the collection</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span> <span class="ruby-comment"># =&gt; 0 # count from database</span>
</pre>
            </div>
          
          
          
            <div class="aka">
              Also aliased as: <a href="CollectionProxy.html#method-i-new">new</a>
            </div>
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-build_source')" id="l_method-i-build_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L228" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-build_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 228</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">build</span>(<span class="ruby-identifier">attributes</span> = {}, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">build</span>(<span class="ruby-identifier">attributes</span>, &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-concat">
            
              <b>concat</b>(*records)
            
            <a href="CollectionProxy.html#method-i-concat" name="method-i-concat" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Add one or more records to the collection by setting their foreign keys to
the association’s primary key. Since &lt;&lt; flattens its argument list
and inserts each record, <code>push</code> and <code>concat</code> behave
identically. Returns <code>self</code> so method calls may be chained.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">pets</span> :<span class="ruby-identifier">has_many</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">concat</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Fancy-Fancy'</span>))
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">concat</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Spook'</span>), <span class="ruby-constant">Pet</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Choo-Choo'</span>))
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">id</span> <span class="ruby-comment"># =&gt; 1</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">concat</span>([<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Brain'</span>), <span class="ruby-constant">Pet</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Benny'</span>)])
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 5</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-concat_source')" id="l_method-i-concat_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L303" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-concat_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 303</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">concat</span>(*<span class="ruby-identifier">records</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">concat</span>(*<span class="ruby-identifier">records</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-count">
            
              <b>count</b>(column_name = nil, options = {})
            
            <a href="CollectionProxy.html#method-i-count" name="method-i-count" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Count all records using SQL.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-count_source')" id="l_method-i-count_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L673" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-count_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 673</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">count</span>(<span class="ruby-identifier">column_name</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">count</span>(<span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">options</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-create">
            
              <b>create</b>(attributes = {}, &amp;block)
            
            <a href="CollectionProxy.html#method-i-create" name="method-i-create" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a new object of the collection type that has been instantiated with
attributes, linked to this object and that has already been saved (if it
passes the validations).</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">create</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Fancy-Fancy'</span>)
<span class="ruby-comment"># =&gt; #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">create</span>([{<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Spook'</span>}, {<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Choo-Choo'</span>}])
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span>  <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span> <span class="ruby-comment"># =&gt; 3</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-create_source')" id="l_method-i-create_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L259" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-create_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 259</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">create</span>(<span class="ruby-identifier">attributes</span> = {}, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">create</span>(<span class="ruby-identifier">attributes</span>, &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-create-21">
            
              <b>create!</b>(attributes = {}, &amp;block)
            
            <a href="CollectionProxy.html#method-i-create-21" name="method-i-create-21" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Like <code>create</code>, except that if the record is invalid, raises an
exception.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Pet</span>
  <span class="ruby-identifier">validates</span> :<span class="ruby-identifier">name</span>, <span class="ruby-identifier">presence</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">create!</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>)
<span class="ruby-comment"># =&gt; ActiveRecord::RecordInvalid: Validation failed: Name can't be blank</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-create-21_source')" id="l_method-i-create-21_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L275" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-create-21_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 275</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">create!</span>(<span class="ruby-identifier">attributes</span> = {}, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">create!</span>(<span class="ruby-identifier">attributes</span>, &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-delete">
            
              <b>delete</b>(*records)
            
            <a href="CollectionProxy.html#method-i-delete" name="method-i-delete" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Deletes the <code>records</code> supplied and removes them from the
collection. For <code>has_many</code> associations, the deletion is done
according to the strategy specified by the <code>:dependent</code> option.
Returns an array with the deleted records.</p>

<p>If no <code>:dependent</code> option is given, then it will follow the
default strategy. The default strategy is <code>:nullify</code>. This sets
the foreign keys to <code>NULL</code>. For, <code>has_many</code>
<code>:through</code>, the default strategy is <code>delete_all</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span> <span class="ruby-comment"># dependent: :nullify option by default</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>))
<span class="ruby-comment"># =&gt; [#&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 2</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>)
<span class="ruby-comment"># =&gt; #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: nil&gt;</span>
</pre>

<p>If it is set to <code>:destroy</code> all the <code>records</code> are
removed by calling their <code>destroy</code> method. See
<code>destroy</code> for more information.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>, <span class="ruby-identifier">dependent</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">destroy</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>), <span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">3</span>))
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 1</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [#&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;]</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>, <span class="ruby-value">3</span>)
<span class="ruby-comment"># =&gt; ActiveRecord::RecordNotFound: Couldn't find all Pets with IDs (1, 3)</span>
</pre>

<p>If it is set to <code>:delete_all</code>, all the <code>records</code> are
deleted <strong>without</strong> calling their <code>destroy</code> method.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>, <span class="ruby-identifier">dependent</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">delete_all</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>))
<span class="ruby-comment"># =&gt; [#&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 2</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>)
<span class="ruby-comment"># =&gt; ActiveRecord::RecordNotFound: Couldn't find Pet with id=1</span>
</pre>

<p>You can pass <code>Fixnum</code> or <code>String</code> values, it finds
the records responding to the <code>id</code> and executes delete on them.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-string">&quot;1&quot;</span>)
<span class="ruby-comment"># =&gt; [#&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">2</span>, <span class="ruby-value">3</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-delete_source')" id="l_method-i-delete_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L565" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-delete_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 565</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">delete</span>(*<span class="ruby-identifier">records</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">delete</span>(*<span class="ruby-identifier">records</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-delete_all">
            
              <b>delete_all</b>()
            
            <a href="CollectionProxy.html#method-i-delete_all" name="method-i-delete_all" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Deletes all the records from the collection. For <code>has_many</code>
associations, the deletion is done according to the strategy specified by
the <code>:dependent</code> option. Returns an array with the deleted
records.</p>

<p>If no <code>:dependent</code> option is given, then it will follow the
default strategy. The default strategy is <code>:nullify</code>. This sets
the foreign keys to <code>NULL</code>. For, <code>has_many</code>
<code>:through</code>, the default strategy is <code>delete_all</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span> <span class="ruby-comment"># dependent: :nullify option by default</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete_all</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>      <span class="ruby-comment"># =&gt; []</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: nil&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: nil&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: nil&gt;</span>
<span class="ruby-comment">#    ]</span>
</pre>

<p>If it is set to <code>:destroy</code> all the objects from the collection
are removed by calling their <code>destroy</code> method. See
<code>destroy</code> for more information.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>, <span class="ruby-identifier">dependent</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">destroy</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete_all</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>)
<span class="ruby-comment"># =&gt; ActiveRecord::RecordNotFound</span>
</pre>

<p>If it is set to <code>:delete_all</code>, all the objects are deleted
<strong>without</strong> calling their <code>destroy</code> method.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>, <span class="ruby-identifier">dependent</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">delete_all</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete_all</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>)
<span class="ruby-comment"># =&gt; ActiveRecord::RecordNotFound</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-delete_all_source')" id="l_method-i-delete_all_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L421" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-delete_all_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 421</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">delete_all</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">delete_all</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-destroy">
            
              <b>destroy</b>(*records)
            
            <a href="CollectionProxy.html#method-i-destroy" name="method-i-destroy" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Destroys the <code>records</code> supplied and removes them from the
collection. This method will <em>always</em> remove record from the
database ignoring the <code>:dependent</code> option. Returns an array with
the removed records.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">destroy</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>))
<span class="ruby-comment"># =&gt; [#&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 2</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">destroy</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">2</span>), <span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">3</span>))
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span>  <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>       <span class="ruby-comment"># =&gt; []</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>) <span class="ruby-comment"># =&gt; ActiveRecord::RecordNotFound: Couldn't find all Pets with IDs (1, 2, 3)</span>
</pre>

<p>You can pass <code>Fixnum</code> or <code>String</code> values, it finds
the records responding to the <code>id</code> and then deletes them from
the database.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 4, name: &quot;Benny&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 5, name: &quot;Brain&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 6, name: &quot;Boss&quot;,  person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">destroy</span>(<span class="ruby-string">&quot;4&quot;</span>)
<span class="ruby-comment"># =&gt; #&lt;Pet id: 4, name: &quot;Benny&quot;, person_id: 1&gt;</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 2</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 5, name: &quot;Brain&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 6, name: &quot;Boss&quot;,  person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">destroy</span>(<span class="ruby-value">5</span>, <span class="ruby-value">6</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 5, name: &quot;Brain&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 6, name: &quot;Boss&quot;,  person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span>  <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>       <span class="ruby-comment"># =&gt; []</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">4</span>, <span class="ruby-value">5</span>, <span class="ruby-value">6</span>) <span class="ruby-comment"># =&gt; ActiveRecord::RecordNotFound: Couldn't find all Pets with IDs (4, 5, 6)</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-destroy_source')" id="l_method-i-destroy_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L637" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-destroy_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 637</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">destroy</span>(*<span class="ruby-identifier">records</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">destroy</span>(*<span class="ruby-identifier">records</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-destroy_all">
            
              <b>destroy_all</b>()
            
            <a href="CollectionProxy.html#method-i-destroy_all" name="method-i-destroy_all" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Deletes the records of the collection directly from the database. This will
<em>always</em> remove the records ignoring the <code>:dependent</code>
option.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">destroy_all</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>      <span class="ruby-comment"># =&gt; []</span>

<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>) <span class="ruby-comment"># =&gt; Couldn't find Pet with id=1</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-destroy_all_source')" id="l_method-i-destroy_all_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L447" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-destroy_all_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 447</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">destroy_all</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">destroy_all</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-distinct">
            
              <b>distinct</b>()
            
            <a href="CollectionProxy.html#method-i-distinct" name="method-i-distinct" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Specifies whether the records should be unique or not.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">select</span>(:<span class="ruby-identifier">name</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet name: &quot;Fancy-Fancy&quot;&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet name: &quot;Fancy-Fancy&quot;&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">select</span>(:<span class="ruby-identifier">name</span>).<span class="ruby-identifier">distinct</span>
<span class="ruby-comment"># =&gt; [#&lt;Pet name: &quot;Fancy-Fancy&quot;&gt;]</span>
</pre>
            </div>
          
          
          
            <div class="aka">
              Also aliased as: <a href="CollectionProxy.html#method-i-uniq">uniq</a>
            </div>
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-distinct_source')" id="l_method-i-distinct_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L655" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-distinct_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 655</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">distinct</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">distinct</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-empty-3F">
            
              <b>empty?</b>()
            
            <a href="CollectionProxy.html#method-i-empty-3F" name="method-i-empty-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns <code>true</code> if the collection is empty. If the collection has
been loaded or the <code>:counter_sql</code> option is provided, it is
equivalent to <code>collection.size.zero?</code>. If the collection has not
been loaded, it is equivalent to <code>collection.exists?</code>. If the
collection has not already been loaded and you are going to fetch the
records anyway it is better to check <code>collection.length.zero?</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span>  <span class="ruby-comment"># =&gt; 1</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-comment"># =&gt; false</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">delete_all</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span>  <span class="ruby-comment"># =&gt; 0</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-comment"># =&gt; true</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-empty-3F_source')" id="l_method-i-empty-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L747" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-empty-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 747</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">empty?</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">empty?</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-find">
            
              <b>find</b>(*args, &amp;block)
            
            <a href="CollectionProxy.html#method-i-find" name="method-i-find" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Finds an object in the collection responding to the <code>id</code>. Uses
the same rules as <code>ActiveRecord::Base.find</code>. Returns
<code>ActiveRecord::RecordNotFound</code> error if the object can not be
found.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">1</span>) <span class="ruby-comment"># =&gt; #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">4</span>) <span class="ruby-comment"># =&gt; ActiveRecord::RecordNotFound: Couldn't find Pet with id=4</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">2</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">pet</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pet</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">downcase!</span> }
<span class="ruby-comment"># =&gt; #&lt;Pet id: 2, name: &quot;fancy-fancy&quot;, person_id: 1&gt;</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">2</span>, <span class="ruby-value">3</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-find_source')" id="l_method-i-find_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L140" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-find_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 140</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">find</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">find</span>(*<span class="ruby-identifier">args</span>, &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-first">
            
              <b>first</b>(*args)
            
            <a href="CollectionProxy.html#method-i-first" name="method-i-first" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the first record, or the first <code>n</code> records, from the
collection. If the collection is empty, the first form returns
<code>nil</code>, and the second form returns an empty array.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">first</span> <span class="ruby-comment"># =&gt; #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">first</span>(<span class="ruby-value">2</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">another_person_without</span>.<span class="ruby-identifier">pets</span>          <span class="ruby-comment"># =&gt; []</span>
<span class="ruby-identifier">another_person_without</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">first</span>    <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">another_person_without</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">first</span>(<span class="ruby-value">3</span>) <span class="ruby-comment"># =&gt; []</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-first_source')" id="l_method-i-first_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L170" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-first_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 170</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">first</span>(*<span class="ruby-identifier">args</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">first</span>(*<span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-include-3F">
            
              <b>include?</b>(record)
            
            <a href="CollectionProxy.html#method-i-include-3F" name="method-i-include-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns <code>true</code> if the given object is present in the collection.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span> <span class="ruby-comment"># =&gt; [#&lt;Pet id: 20, name: &quot;Snoop&quot;&gt;]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">20</span>)) <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">21</span>)) <span class="ruby-comment"># =&gt; false</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-include-3F_source')" id="l_method-i-include-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L832" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-include-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 832</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">include?</span>(<span class="ruby-identifier">record</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">record</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-last">
            
              <b>last</b>(*args)
            
            <a href="CollectionProxy.html#method-i-last" name="method-i-last" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the last record, or the last <code>n</code> records, from the
collection. If the collection is empty, the first form returns
<code>nil</code>, and the second form returns an empty array.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">last</span> <span class="ruby-comment"># =&gt; #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">last</span>(<span class="ruby-value">2</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">another_person_without</span>.<span class="ruby-identifier">pets</span>         <span class="ruby-comment"># =&gt; []</span>
<span class="ruby-identifier">another_person_without</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">last</span>    <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">another_person_without</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">last</span>(<span class="ruby-value">3</span>) <span class="ruby-comment"># =&gt; []</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-last_source')" id="l_method-i-last_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L200" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-last_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 200</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">last</span>(*<span class="ruby-identifier">args</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">last</span>(*<span class="ruby-identifier">args</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-length">
            
              <b>length</b>()
            
            <a href="CollectionProxy.html#method-i-length" name="method-i-length" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the size of the collection calling <code>size</code> on the target.
If the collection has been already loaded, <code>length</code> and
<code>size</code> are equivalent. If not and you are going to need the
records anyway this method will take one less query. Otherwise
<code>size</code> is more efficient.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">length</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-comment"># executes something like SELECT &quot;pets&quot;.* FROM &quot;pets&quot; WHERE &quot;pets&quot;.&quot;person_id&quot; = 1</span>

<span class="ruby-comment"># Because the collection is loaded, you can</span>
<span class="ruby-comment"># call the collection with no additional queries:</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-length_source')" id="l_method-i-length_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L725" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-length_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 725</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">length</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">length</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-load_target">
            
              <b>load_target</b>()
            
            <a href="CollectionProxy.html#method-i-load_target" name="method-i-load_target" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-load_target_source')" id="l_method-i-load_target_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L44" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-load_target_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 44</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">load_target</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">load_target</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-loaded-3F">
            
              <b>loaded?</b>()
            
            <a href="CollectionProxy.html#method-i-loaded-3F" name="method-i-loaded-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns <code>true</code> if the association has been loaded, otherwise
<code>false</code>.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">loaded?</span> <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">loaded?</span> <span class="ruby-comment"># =&gt; true</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-loaded-3F_source')" id="l_method-i-loaded-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L53" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-loaded-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 53</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">loaded?</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">loaded?</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-many-3F">
            
              <b>many?</b>(&amp;block)
            
            <a href="CollectionProxy.html#method-i-many-3F" name="method-i-many-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns true if the collection has more than one record. Equivalent to
<code>collection.size &gt; 1</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span> <span class="ruby-comment">#=&gt; 1</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">many?</span> <span class="ruby-comment">#=&gt; false</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Pet</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Snoopy'</span>)
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">count</span> <span class="ruby-comment">#=&gt; 2</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">many?</span> <span class="ruby-comment">#=&gt; true</span>
</pre>

<p>You can also pass a block to define criteria. The behavior is the same, it
returns true if the collection based on the criteria has more than one
record.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet name: &quot;Gorby&quot;, group: &quot;cats&quot;&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet name: &quot;Puff&quot;, group: &quot;cats&quot;&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet name: &quot;Snoop&quot;, group: &quot;dogs&quot;&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">many?</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">pet</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">pet</span>.<span class="ruby-identifier">group</span> <span class="ruby-operator">==</span> <span class="ruby-string">'dogs'</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># =&gt; false</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">many?</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">pet</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">pet</span>.<span class="ruby-identifier">group</span> <span class="ruby-operator">==</span> <span class="ruby-string">'cats'</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># =&gt; true</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-many-3F_source')" id="l_method-i-many-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L818" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-many-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 818</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">many?</span>(&amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">many?</span>(&amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-new">
            
              <b>new</b>(attributes = {}, &amp;block)
            
            <a href="CollectionProxy.html#method-i-new" name="method-i-new" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-proxy_association">
            
              <b>proxy_association</b>()
            
            <a href="CollectionProxy.html#method-i-proxy_association" name="method-i-proxy_association" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-proxy_association_source')" id="l_method-i-proxy_association_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L836" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-proxy_association_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 836</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">proxy_association</span>
  <span class="ruby-ivar">@association</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-replace">
            
              <b>replace</b>(other_array)
            
            <a href="CollectionProxy.html#method-i-replace" name="method-i-replace" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Replaces this collection with <code>other_array</code>. This will perform a
diff and delete/add only records that have changed.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [#&lt;Pet id: 1, name: &quot;Gorby&quot;, group: &quot;cats&quot;, person_id: 1&gt;]</span>

<span class="ruby-identifier">other_pets</span> = [<span class="ruby-constant">Pet</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Puff'</span>, <span class="ruby-identifier">group</span><span class="ruby-operator">:</span> <span class="ruby-string">'celebrities'</span>]

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">replace</span>(<span class="ruby-identifier">other_pets</span>)

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [#&lt;Pet id: 2, name: &quot;Puff&quot;, group: &quot;celebrities&quot;, person_id: 1&gt;]</span>
</pre>

<p>If the supplied array has an incorrect association type, it raises an
<code>ActiveRecord::AssociationTypeMismatch</code> error:</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">replace</span>([<span class="ruby-string">&quot;doo&quot;</span>, <span class="ruby-string">&quot;ggie&quot;</span>, <span class="ruby-string">&quot;gaga&quot;</span>])
<span class="ruby-comment"># =&gt; ActiveRecord::AssociationTypeMismatch: Pet expected, got String</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-replace_source')" id="l_method-i-replace_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L329" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-replace_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 329</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">replace</span>(<span class="ruby-identifier">other_array</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">replace</span>(<span class="ruby-identifier">other_array</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-scope">
            
              <b>scope</b>()
            
            <a href="CollectionProxy.html#method-i-scope" name="method-i-scope" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a <code>Relation</code> object for the records in this association</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-scope_source')" id="l_method-i-scope_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L849" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-scope_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 849</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">scope</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">scope</span>.<span class="ruby-identifier">tap</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">scope</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">scope</span>.<span class="ruby-identifier">proxy_association</span> = <span class="ruby-ivar">@association</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-scoping">
            
              <b>scoping</b>()
            
            <a href="CollectionProxy.html#method-i-scoping" name="method-i-scoping" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>We don’t want this object to be put on the scoping stack, because that
could create an infinite loop where we call an @association method, which
gets the current scope, which is this object, which delegates to
@association, and so on.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-scoping_source')" id="l_method-i-scoping_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L844" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-scoping_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 844</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">scoping</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">scope</span>.<span class="ruby-identifier">scoping</span> { <span class="ruby-keyword">yield</span> }
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-select">
            
              <b>select</b>(select = nil, &amp;block)
            
            <a href="CollectionProxy.html#method-i-select" name="method-i-select" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Works in two ways.</p>

<p><strong>First:</strong> Specify a subset of fields to be selected from the
result set.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">select</span>(:<span class="ruby-identifier">name</span>)
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet id: nil, name: &quot;Fancy-Fancy&quot;&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: nil, name: &quot;Spook&quot;&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: nil, name: &quot;Choo-Choo&quot;&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">select</span>([:<span class="ruby-identifier">id</span>, :<span class="ruby-identifier">name</span>])
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: 2, name: &quot;Spook&quot;&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;&gt;</span>
<span class="ruby-comment">#    ]</span>
</pre>

<p>Be careful because this also means you’re initializing a model object with
only the fields that you’ve selected. If you attempt to access a field that
is not in the initialized record you’ll receive:</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">select</span>(:<span class="ruby-identifier">name</span>).<span class="ruby-identifier">first</span>.<span class="ruby-identifier">person_id</span>
<span class="ruby-comment"># =&gt; ActiveModel::MissingAttributeError: missing attribute: person_id</span>
</pre>

<p><strong>Second:</strong> You can pass a block so it can be used just like
Array#select. This builds an array of objects from the database for the
scope, converting them into an array and iterating through them using
Array#select.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">select</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">pet</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pet</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/oo/</span> }
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">select</span>(:<span class="ruby-identifier">name</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">pet</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pet</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/oo/</span> }
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#      #&lt;Pet id: 2, name: &quot;Spook&quot;&gt;,</span>
<span class="ruby-comment">#      #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;&gt;</span>
<span class="ruby-comment">#    ]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-select_source')" id="l_method-i-select_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L110" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-select_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 110</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">select</span>(<span class="ruby-identifier">select</span> = <span class="ruby-keyword">nil</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">select</span>(<span class="ruby-identifier">select</span>, &amp;<span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-size">
            
              <b>size</b>()
            
            <a href="CollectionProxy.html#method-i-size" name="method-i-size" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the size of the collection. If the collection hasn’t been loaded,
it executes a <code>SELECT COUNT(*)</code> query. Else it calls
<code>collection.size</code>.</p>

<p>If the collection has been already loaded <code>size</code> and
<code>length</code> are equivalent. If not and you are going to need the
records anyway <code>length</code> will take one less query. Otherwise
<code>size</code> is more efficient.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">has_many</span> :<span class="ruby-identifier">pets</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-comment"># executes something like SELECT COUNT(*) FROM &quot;pets&quot; WHERE &quot;pets&quot;.&quot;person_id&quot; = 1</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span> <span class="ruby-comment"># This will execute a SELECT * FROM query</span>
<span class="ruby-comment"># =&gt; [</span>
<span class="ruby-comment">#       #&lt;Pet id: 1, name: &quot;Fancy-Fancy&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 2, name: &quot;Spook&quot;, person_id: 1&gt;,</span>
<span class="ruby-comment">#       #&lt;Pet id: 3, name: &quot;Choo-Choo&quot;, person_id: 1&gt;</span>
<span class="ruby-comment">#    ]</span>

<span class="ruby-identifier">person</span>.<span class="ruby-identifier">pets</span>.<span class="ruby-identifier">size</span> <span class="ruby-comment"># =&gt; 3</span>
<span class="ruby-comment"># Because the collection is already loaded, this will behave like</span>
<span class="ruby-comment"># collection.size and no SQL count query is executed.</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-size_source')" id="l_method-i-size_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L701" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-size_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 701</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">size</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">size</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-target">
            
              <b>target</b>()
            
            <a href="CollectionProxy.html#method-i-target" name="method-i-target" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-target_source')" id="l_method-i-target_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b15ce4a006756a0b6cacfb9593d88c9a7dfd8eb0/activerecord/lib/active_record/associations/collection_proxy.rb#L40" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-target_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/associations/collection_proxy.rb, line 40</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">target</span>
  <span class="ruby-ivar">@association</span>.<span class="ruby-identifier">target</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-uniq">
            
              <b>uniq</b>()
            
            <a href="CollectionProxy.html#method-i-uniq" name="method-i-uniq" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
          </div>
                    </div>

    </div>
  </body>
</html>    